package com.trkj.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;
import com.trkj.dao.*;
import com.trkj.pojos.*;

/**
 * 业务类
 */
@Service
@Transactional
public class AccountService {
	@Autowired
	private IAccountDAO accountDAO;
	@Autowired
	private ITransferDAO itransferDAO;

	/**
	 * 1、添加业务<br>
	 */
	public void createUser(String name, Double openMoney, String password) {
		Account account = new Account();
		account.setBalance(openMoney);
		account.setOpenMoney(openMoney);
		account.setOpenTime(new Date());
		account.setPassword(password);
		account.setName(name);
		accountDAO.insertAccout(account);

	}

	/**
	 * 2、修改密码业务<br>
	 */
	public void update(Integer id, String password, String newPassword) {
		Account account = accountDAO.findById(id);
		if (password.equals(account.getPassword())) {
			account.setPassword(newPassword);
			accountDAO.update(account);
			System.out.println("密码修改成功！");
		} else {
			System.out.println("密码修改失败！");
		}
	}

	/**
	 * 3、传入=支出？转入：即可操作<br>
	 * 
	 */
	public void busIness(Integer id, Double money, String password, String operAtion) {
		Account account = accountDAO.findById(id);
		System.out.println(operAtion);
		System.out.println(account.toString());
		boolean i = false;
		if (password.equals(account.getPassword())) {
			if (operAtion.equals("转入")) {
				account.setBalance(account.getBalance() + money);
				i = true;
			} else if (operAtion.equals("支出") && account.getBalance() >= money) {
				account.setBalance(account.getBalance() - money);
				i = true;
			}
			if (i) {
				Tranfer transfer = new Tranfer();
				transfer.setaID(account.getAID());
				transfer.setMonet(money);
				transfer.setType(operAtion);
				transfer.setTime(new Date());
				itransferDAO.insert(transfer);
				accountDAO.update(account);
				System.out.println(operAtion + "操作成功！");
			} else {
				System.out.println(operAtion + "操作失败！");
			}
		} else {
			System.out.println("密码错误操作失败！");
		}
	}
}
